package fightplan.persistence;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

public class ImportHelper {

	static String normalizeName(String fighterName) {
		fighterName = fighterName.trim();
		boolean first = true;
		StringBuilder builder = new StringBuilder(fighterName.length());
		for (int i = 0; i < fighterName.length(); i++) {
			char c = fighterName.charAt(i);
			if (Character.isWhitespace(c)) {
				first = true;
			} else if (!first && Character.isUpperCase(c)) {
				c = Character.toLowerCase(c);
				first = false;
			} else {
				first = false;
			}
			builder.append(c);
		}
		return builder.toString();
	}

	// Cell Helper

	static String getString(HSSFRow row, int column) {
		HSSFCell cell = row.getCell((short) column);
		if (cell == null) {
			return null;
		} else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
			return "" + (int) cell.getNumericCellValue();
		} else {
			return cell.getStringCellValue().trim();
		}
	}

	static boolean getBoolean(HSSFRow row, int column) {
		HSSFCell cell = row.getCell((short) column);
		if (cell == null) {
			return false;
		} else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
			return cell.getNumericCellValue() > 0;
		} else {
			return false;
		}
	}

}
